/**
 * Copyright (c) 2011-9-23 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.task.file;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Properties;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.util.Configuration;
import org.wkm.util.Home;

import static org.wkm.util.CharsetUtil.*;
import static org.wkm.util.Configuration.*;

/**
 * @author WKM Modification history 1.WKM -2011-9-23-Create File
 */
public class DataReader {
	private static Logger logger = LoggerFactory.getLogger(DataReader.class);
	private static DataReader DR = null;

	private String name = null;
	
	private DataReader(String name) {
		try {
			this.name = name;
			read();
		} catch (IOException e) {
			// TODO: handle exception
			logger.info("读取配置文件失败:" + CONFIG_CONFIG);
			e.printStackTrace();
		}
	}

	public static DataReader getInstance(String name) {
		if (DR == null) {
			DR = new DataReader(name);
		}
		return DR;
	}
	
	
	public static DataReader getInstance() {
		if (DR == null) {
			DR = new DataReader(CONFIG_CONFIG);
		}
		return DR;
	}

	private Properties property = new Properties();

	private void read() throws IOException {

		InputStream inStream = getClass().getClassLoader().getResourceAsStream(
				name);

		if (inStream != null) {
			logger.info("读取配置文件成功:" + name);
			property.load(inStream);
			inStream.close();
		} 
		File configFile = new File(Home.getPath(Configuration.CONFIG_CONFIG));
		if(!configFile.exists()){
			logger.info("读取配置文件失败:" + Home.getPath(Configuration.CONFIG_CONFIG));
			return ;
		}
		inStream = new FileInputStream(configFile);
		logger.info("读取配置文件:" + Home.getPath(Configuration.CONFIG_CONFIG));
		if (inStream != null) {
			logger.info("读取配置文件成功:" + Home.getPath(Configuration.CONFIG_CONFIG));
			property.load(inStream);
			inStream.close();
		} 
	}

	public String getProperty(String key) {
		return getProperty(key, UTF_8);
	}

	public String getProperty(String key, String codec) {
		return getProperty(key, ISO_8859_1, codec);
	}

	public String getProperty(String key, String fromCode, String codec) {
		try {
			return new String(property.getProperty(key).getBytes(fromCode),
					codec);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			return property.getProperty(key);
		}
	}

	public Image getImage(String name) {
		try {
			File file = getImageFile(name);
			return ImageIO.read(file);
		} catch (IOException e) {
			// TODO: handle exception
			e.getMessage();
			logger.error(e.getMessage());
			logger.error("不能找到:" + name);
		}
		return null;
	}
	
	public File getImageFile(String name) {
		return new File(Home.getPath(IMAGES_FRAME + name));
	}

	public static void main(String[] args) throws Exception {
		try {
//			new DataReader().read();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR, 4);
		System.out.println(c.get(Calendar.DAY_OF_WEEK));
		System.out.println(c.get(Calendar.HOUR_OF_DAY));
	}
}
